import 'package:backend.core/core/domain/entities/google_play/google_play_purchased_subscription_transaction.dart';
import 'package:backend.purchases/domain/entities/google_play_api/google_play_api_revoked_purchased_item.dart';
import 'package:backend.purchases/domain/helpers/google_play/create_google_play_transaction_revocation_info_from_api_revoked_purchased_item.dart';

abstract class UpdateGooglePlayPurchasedSubscriptionTransactionFromRevokedPurchasedItem {
  GooglePlayPurchasedSubscriptionTransaction call({
    required GooglePlayPurchasedSubscriptionTransaction purchasedSubscriptionTransaction,
    required GooglePlayApiRevokedPurchasedItem revokedPurchasedItem,
  });
}

class UpdateGooglePlayPurchasedSubscriptionTransactionFromRevokedPurchasedItemImpl
    implements UpdateGooglePlayPurchasedSubscriptionTransactionFromRevokedPurchasedItem {
  const UpdateGooglePlayPurchasedSubscriptionTransactionFromRevokedPurchasedItemImpl({
    required CreateGooglePlayTransactionRevocationInfoFromApiRevokedPurchasedItem
        createGooglePlayTransactionRevocationInfoFromApiRevokedPurchasedItem,
  }) : _createGooglePlayTransactionRevocationInfoFromApiRevokedPurchasedItem =
            createGooglePlayTransactionRevocationInfoFromApiRevokedPurchasedItem;

  final CreateGooglePlayTransactionRevocationInfoFromApiRevokedPurchasedItem
      _createGooglePlayTransactionRevocationInfoFromApiRevokedPurchasedItem;

  @override
  GooglePlayPurchasedSubscriptionTransaction call({
    required GooglePlayPurchasedSubscriptionTransaction purchasedSubscriptionTransaction,
    required GooglePlayApiRevokedPurchasedItem revokedPurchasedItem,
  }) {
    if (purchasedSubscriptionTransaction.orderId == revokedPurchasedItem.orderId) {
      final updatedRevocationInfo =
          _createGooglePlayTransactionRevocationInfoFromApiRevokedPurchasedItem(
        revokedPurchasedItem: revokedPurchasedItem,
      );

      return purchasedSubscriptionTransaction.copyWith(
        revocationInfo: () => updatedRevocationInfo,
      );
    } else {
      return purchasedSubscriptionTransaction;
    }
  }
}
